/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021-03-23
 * Time: 18:04
 */
#include "DiagramBaseItem.h"
#include "Arrow.h"
#include "PropertyDialog.h"

DiagramBaseItem::DiagramBaseItem(const QString &item_title, bool is_root, QGraphicsItem *parent)
        : QGraphicsPolygonItem(parent)

          , resize_pressed_(false)
          , is_root_(is_root)
          , place_type_(PlaceType::Vertical)
          , item_title_(item_title)
          , in_point_(0, 0)
          , out_point_(0, 0)
{
    InitUI();
    InitEvent();
    InitProperty();
    UpdateConnectPoint();

    text_item_ = new DiagramBasePrivateText(item_title,this);
}

DiagramBaseItem::~DiagramBaseItem()
{
    children_items_.clear();
//    parent_items_.clear();
}

/*void DiagramBaseItem::AddArrow(Arrow *arrow)
{
    arrows_.append(arrow);
}

void DiagramBaseItem::RemoveArrow(Arrow *arrow)
{
    int index = arrows_.indexOf(arrow);

    if (index != -1)
        arrows_.removeAt(index);
}

void DiagramBaseItem::RemoveArrows()
{
    for (Arrow *arrow : arrows_) {
        arrow->StartItem()->RemoveArrow(arrow);
        arrow->EndItem()->RemoveArrow(arrow);
        scene()->removeItem(arrow);
        delete arrow;
    }
}*/

void DiagramBaseItem::InitUI()
{
    /*[NOTE] 2021/3/23 19:11 loong:自定义Item的绘制类型 */
    item_polygon_ << QPointF(-25, -50) << QPointF(25, -50)
                  << QPointF(25, 50) << QPointF(-25, 50)
                  << QPointF(-25, -50);

    setPolygon(item_polygon_);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    setFlag(QGraphicsItem::ItemContainsChildrenInShape, true);
}

void DiagramBaseItem::InitEvent()
{

}

void DiagramBaseItem::InitProperty()
{
    QMetaEnum m = QMetaEnum::fromType<PlaceType>();

    setData(IDData,QUuid::createUuid().toString());
    setData(ParentIDData,"");
    setData(TitleData,item_title_);
    setData(IsRootData,IsRoot());
    setData(PlaceTypeData,m.key(GetPlaceType()));
    setData(PosXData,this->pos().x());
    setData(PosYData,this->pos().y());
    setData(WidthData,this->polygon().boundingRect().width());
    setData(HeightData,this->polygon().boundingRect().height());
}

void DiagramBaseItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    if(this->isSelected())
        resize_pressed_ = true;
    QGraphicsItem::hoverMoveEvent(event);
}

void DiagramBaseItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    this->update();
    if (resize_pressed_) {
        /*[TODO] 2021/3/25 18:56 loong:在此添加大小改变逻辑，将该类型的item_polygon_内容根据鼠标位置进行改变 */
    }
    else
        QGraphicsItem::mouseMoveEvent(event);
  
}

void DiagramBaseItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    PropertyDialog dialog(this);
    dialog.exec();
    QGraphicsItem::mouseDoubleClickEvent(event);
}

void DiagramBaseItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    /*[]: 记录鼠标的位置，用于指定位置时的鼠标吸附 */
    QPointF hoverPos = event->pos();//返回的item中的坐标
    int adjustSize = 0.5;

    QGraphicsItem::mousePressEvent(event);
}

void DiagramBaseItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(this->isSelected()) {
        /*[1]: 绘polygan矩形框 */
        QRectF rect = this->polygon().boundingRect();

        painter->setPen(QPen(Qt::green, 1, Qt::DashLine));
        painter->setBrush(Qt::NoBrush);
        painter->drawRect(rect);

        /*[2]: 计算顶部和底部的连接点 */
        //QPointF out_point_, in_point_;
        QRectF topCenterRect, bottomCenterRect;

        topCenterRect = QRectF((out_point_ - QPointF(3, 3)), QSizeF(6, 6));
        bottomCenterRect = QRectF((in_point_ - QPointF(3, 3)), QSizeF(6, 6));


        /*[3]: 绘制顶部和底部的连接点 */
        painter->setPen(QPen(Qt::black));
        painter->setBrush(Qt::green);
        painter->drawRect(topCenterRect);
        painter->drawRect(bottomCenterRect);
    }

    /*[3]: 绘制item的title */
    //painter->drawText(this->boundingRect(), Qt::AlignCenter | Qt::TextWrapAnywhere, item_title_);
    QGraphicsPolygonItem::paint(painter, option, widget);

}

void DiagramBaseItem::UpdateConnectPoint()
{
    QRectF rect = this->polygon().boundingRect();

    out_point_ = QPointF(rect.center().x(), rect.top());
    in_point_ = QPointF(rect.center().x(), rect.bottom());
}

QRectF DiagramBaseItem::boundingRect() const
{
    return this->polygon().boundingRect().adjusted(-5,-5,5,5);
}

void DiagramBaseItem::RotateItem()
{
    /*[TODO] 2021/3/26 17:00 loong:旋转item,重新计算坐标和绘制连接点 */
    place_type_ = place_type_ == Vertical ? Horizontal : Vertical;

    item_polygon_.clear();

    switch (place_type_) {

        case Horizontal:
            item_polygon_ << QPointF(-50, -25) << QPointF(50, -25)
                          << QPointF(50, 25) << QPointF(-50, 25)
                          << QPointF(-50, -25);
            break;
        case Vertical:
            item_polygon_ << QPointF(-25, -50) << QPointF(25, -50)
                          << QPointF(25, 50) << QPointF(-25, 50)
                          << QPointF(-25, -50);
            break;
    }
    setPolygon(item_polygon_);

    UpdateConnectPoint();
}

void DiagramBaseItem::SetItemPos(QPointF pos)
{

}

void DiagramBaseItem::SetItemSize(QSizeF size)
{

}

QVariant DiagramBaseItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    UpdateProperty();

    return QGraphicsItem::itemChange(change, value);
}

void DiagramBaseItem::UpdateProperty()
{
    QMetaEnum m = QMetaEnum::fromType<PlaceType>();

    setData(IDData, this->data(IDData).toString());
    setData(ParentIDData, "");
    setData(TitleData, this->data(TitleData).toString());
    setData(IsRootData, IsRoot());
    setData(PlaceTypeData, m.key(GetPlaceType()));
    setData(PosXData, pos().x());
    setData(PosYData, pos().y());
    setData(WidthData, polygon().boundingRect().width());
    setData(HeightData, polygon().boundingRect().height());
    setData(UnitRectData, this->data(UnitRectData));
}



